<!--
 * @Author: zhangjingyu
 * @Date: 2021-04-12 17:57:14
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-05-14 09:59:09
 * @FilePath: \vprisk10.14\src\views\ftpManage\pricingManage\hisBusiRepricing\rp_executeNotes.vue
 * @Description: zh
-->
<template>
  <!-- 重定价执行记录 -->
  <div>
    <span class="formTitle"><i class="vertical-bar"></i>重定价执行记录</span>
    <upper-template
      @search="(searchForm.page = 1), searchFunc()"
      @clear="searchForm.dataDate = ''"
      :configIcon="false"
    >
      <div slot="buttonArea">
        <el-button @click="expExcel()">导出</el-button>
        <el-button @click="toSubRecord()">子任务</el-button>
        <el-button @click="goBack()">返回</el-button>
      </div>
      <div slot="searchArea">
        <el-form
          :model="searchForm"
          ref="searchForm"
          label-width="100px"
          label-position="right"
          size="mini"
        >
          <el-form-item label="任务编号：" prop="taskCode">
            <el-input
              v-model="searchForm.taskCode"
              style="width:150px"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="任务名称：" prop="taskName">
            <el-input
              v-model="searchForm.taskName"
              style="width:150px"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="数据日期：" prop="dataDate">
            <el-date-picker
              v-model="searchForm.dataDate"
              type="date"
              style="width:150px;"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </div>
    </upper-template>

    <el-table
      ref="multipleTable"
      :data="tableData"
      style="width: 100%"
      @row-click="rowClick"
    >
      <el-table-column width="55" type="selection"  />

      <el-table-column
        prop="pricingCode"
        label="定价编号"
        show-overflow-tooltip
      />
      <el-table-column prop="dataDate" label="数据日期" show-overflow-tooltip />
      <el-table-column
        prop="sceneCode"
        label="情景编号"
        show-overflow-tooltip
      />
      <el-table-column
        prop="sceneVersion"
        label="情景版本"
        show-overflow-tooltip
      />
      <el-table-column prop="startTm" label="开始时间" show-overflow-tooltip />
      <el-table-column prop="endTm" label="结束时间" show-overflow-tooltip />
      <el-table-column
        prop="successNum"
        label="成功记录"
        show-overflow-tooltip
      />
      <el-table-column prop="failNum" label="失败记录" show-overflow-tooltip />
      <el-table-column prop="status" label="执行状态" show-overflow-tooltip :formatter="formatRunStatus"/>
    </el-table>
    <pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchForm.page"
      :page-size="searchForm.size"
      @refresh="searchFunc"
      :total="total"
    ></pagination>
  </div>
</template>

<script>
import { getDict } from "@/api/getDict";
import { pricingTask } from "@/api/index";
export default {
  props: {
    //选中的值
    baseData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      blocktotal: null,
      total: 0,
      //引擎任务页面
      positionOptions: {},
      statusOption: {},
      blocktitleName: "引擎任务",
      blockModelData: [],
      blockForm: {},
      block: false,
      //定价子任务表单数据
      subRecordModelData: [],
      //定价子任务界面上方数据
      recordForm: { taskName: "", pricingCode: "", dataDate: "" },
      titleName: "定价子任务",
      searchForm: {
        taskCode: this.baseData.taskCode,
        taskName: this.baseData.taskName,
        dataDate: "",
        page: 1,
        size: 15
      },
      tableData: [],
      textData: [
        { prop: "pricingCode", label: "定价编号", disabled: true },
        { prop: "dataDate", label: "数据日期", disabled: true },
        { prop: "sceneCode", label: "情景编号", disabled: true },
        { prop: "sceneVersion", label: "情景版本", disabled: true },
        { prop: "startTm", label: "开始时间", disabled: true },
        { prop: "endTm", label: "结束时间", disabled: true },
        { prop: "successNum", label: "成功记录", disabled: true },
        { prop: "failNum", label: "失败记录", disabled: true },
        { prop: "status", label: "执行记录", disabled: true }
      ],
      checkList: []
    };
  },
  watch: {
    baseData: {
      handler(newV, oldV) {
        this.searchForm.taskCode = this.baseData.taskCode;
        this.searchForm.taskName = this.baseData.taskName;
        this.searchFunc();
      },
      deep: true,
      immediate: true
    }
  },
  components: {},
  created() {
    this.searchFunc();
  },
  computed: {},
  mounted() {},
  methods: {
      formatRunStatus(row, column) {
      if (row.status == "INIT") {
        return "初始化";
      } else if (row.status == "PREPARING") {
        return "准备中";
      } else if (row.status == "RUNNING") {
        return "运行中";
      } else if (row.status == "FINISH") {
        return "已完成";
      } else if (row.status == "ERROR") {
        return "错误";
      } else if (row.status == "SENDBACK") {
        return "已退回";
      } else if (row.status == "RETRY") {
        return "再次尝试中";
      } else {
        return "";
      }
    }, 
    expExcel() {
      let data = {
        taskCode: this.searchForm.taskCode,
        taskName: this.searchForm.taskName,
        dataDate: this.searchForm.dataDate
      };
      pricingTask.excuteRecord(data).then(response => {
           console.log(response,"response");
        if (response.code == 500) {
          this.$message.error(response.message);
        } else if (response) {
          console.log(decodeURI(response.headers["content-disposition"]));
          var filename = decodeURI(
            response.headers["content-disposition"]
          ).split("=")[1];
          let contenttype =
            filename.substring(filename.lastIndexOf("."), filename.length) ===
            "xlsx"
              ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
              : "application/vnd.ms-excel;charset=utf-8";

          var blob = new Blob([response.data], {
            type: contenttype
          });
          if (window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, filename);
          } else {
            var downloadElement = document.createElement("a");
            var href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            downloadElement.download = filename; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          }
          this.$message.success("导出成功");
        } else {
          this.$message.error(response.message || "获取失败");
        }
      });
    },
    handleSizeChange(val) {
      this.searchForm.size = val;
      this.searchFunc();
      console.log(val, "每页数");
    },
    handleCurrentChange(val) {
      this.searchForm.page = val;
      this.searchFunc();
      console.log(val, "当前页");
    },
    goBack() {
      this.$emit("backIndex", "");
    },
    getstatus(data) {
      getDict(data).then(res => {
        if (res.code == 200) {
          this.statusOption = res.datas;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    toBlock() {
      this.getstatus("ftp_pricing_task_status");
      let selection = this.$refs.multipleTable.selection;
      if (selection.length != 1) {
        return this.$message.warning("请选择一条数据");
      }
      let data = { pricingCode: selection[0].pricingCode };
      pricingTask
        .toBlockSceneTask('"' + selection[0].pricingCode + '"')
        .then(res => {
          console.log(res, "跳转子任务的时候chaxundaodeshujv==============");
          if (res.code == 200) {
            this.positionOptions = res.datas.positions;
          }
          console.log(this.recordForm, "recordForm");
        });
      this.blockForm.taskName = this.recordForm.taskName;
      this.blockForm.pricingCode = this.recordForm.pricingCode;
      this.blockForm.dataDate = this.recordForm.dataDate;
      console.log(this.blockForm, "blockForm");
      pricingTask.findAllBlock(data).then(res => {
        console.log(res, "跳转引擎任务的时候chaxundaodeshujv==============");
        if (res.code == 200) {
          this.blockModelData = res.datas.rows;
        }
        console.log(this.blockModelData, "blockModelData");
      });
      this.block = true;
      this.subRecord = false;
    },
    toRecord() {
      this.block = false;
    },
    searchFunc() {
      let data = this.searchForm;
      pricingTask.pricingRecordFindAll(data).then(res => {
        if (res.code == 200) {
          this.tableData = res.datas.rows;
          this.total = res.datas.total;
        }
      });
    },
    toSubRecord() {
      console.log(this.baseData);
      let selection = this.$refs.multipleTable.selection;
      if (selection.length != 1) {
        return this.$message.warning("请选择一条数据");
      }

      let obj = {...this.baseData}
      obj.pricingCode=selection[0].pricingCode
      this.$emit("toSubTasks", obj);
    },
    //点击行选中
    rowClick(row) {
      if (this.$refs.multipleTable) {
        this.$refs.multipleTable.clearSelection();
      }
      this.$refs.multipleTable.toggleRowSelection(row, true);
    }
  }
};
</script>

<style lang="scss" scoped>
.formTitle {
  font-size: 14px;
  font-weight: 700;
  display: block;
  margin-bottom: 10px;
}
</style>